//
//  DynamicVC.m
//  phmall
//
//  Created by admin on 2017/9/5.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "DynamicVC.h"
#import "Orderheader.h"
//#import "CategoryModel.h"
#import "GoodsModel.h"
//上新
#import "UpdateView.h"
//折扣
#import "DiscountView.h"
//商品分类
#import "GoodsCategoryView.h"
#import "CategoryModel.h"
#import "ShopInfoModel.h"
#import "CommodityDetailsVC.h"
@interface DynamicVC ()<UIScrollViewDelegate,UpdateViewDelegate,DiscountViewDelegate,GoodsCategoryViewDelegate,UIGestureRecognizerDelegate>{
    UIScrollView *_scrollView;
    BOOL _isup;
    Orderheader *_headView;
    UpdateView *update;
    DiscountView *discount;
    NSString *newList,*discountList;
    GoodsCategoryView *category;
}


@end

@implementation DynamicVC
-(void)didSelectRowAtIndexPath:(NSString *)sender{
    self.hidesBottomBarWhenPushed=YES;
    CommodityDetailsVC *deta=[[CommodityDetailsVC alloc] init];
    deta.uid=sender;
    [self.navigationController pushViewController:deta animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //隐藏标签栏
    self.navBarBgAlpha=@"1.0";
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    
    self.navigationItem.leftBarButtonItem=leftBar;
    
    self.title=self.shop_name;
    
    [self addVIew];
    [self setUI];
    [self loadNewShop];
    [self loadDiscountShop];
    [self loadDataCycle];
    [self loadDataCategory];
    // Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loadNewShop{
    NSString *urlstr =  [NSString stringWithFormat:@"%@index/%@/1/get_shop_new/lan",HomeURL,self.uid];
    
    [httpTool ZBGetNetDataWith:urlstr withDic:nil andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        [update analysisDataWithNewArray:responseDict[@"data"]];
    } andFailure:^{
       
    }];
}
- (void)loadDiscountShop{
    NSString *urlstr =  [NSString stringWithFormat:@"%@index/%@/1/get_shop_discount/lan",HomeURL,self.uid];
    
    [httpTool ZBGetNetDataWith:urlstr withDic:nil  andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        [discount analysisDataWithDiscountArray:responseDict[@"data"]];
    } andFailure:^{

    }];
    
    
}
#pragma mark - 商品分类请求
- (void)loadDataCategory{
    NSString *urlstr =  [NSString stringWithFormat:@"%@index/%@/1/get_categroy/lan",HomeURL,self.uid];
    [httpTool ZBGetNetDataWith:urlstr  withDic:nil andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        [category analysisDataWithCategoryArray:responseDict[@"data"]];
    } andFailure:^{

    }];
}
#pragma mark - 轮播图数据请求
- (void)loadDataCycle{
    NSString *urlstr =  [NSString stringWithFormat:@"%@index/%@/1/get_shop_info/lan",HomeURL,self.uid];
    [httpTool ZBGetNetDataWith:urlstr withDic:nil andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        [category analysisDataWithCycleDictionary:responseDict[@"data"][0]];
    } andFailure:^{
        
    }];
}

-(void)setUI{
    //消除强引用
    __weak typeof(self) weakSelf = self;
    _headView = [[Orderheader alloc]init];
    [self.view addSubview:_headView];
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view);
        make.height.offset(40);
    }];
    _headView.backgroundColor=[UIColor colorWithHexString:@"#f8fafd"];
    _headView.itemClickAtIndex = ^(NSInteger index){
        [weakSelf adjustScrollView:index];
    };
    _headView.items = @[Localized(@"商品分类"),Localized(@"产品上新"),Localized(@"折扣优惠")];
    
    _scrollView = [[UIScrollView alloc]init];
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    _scrollView.backgroundColor=[UIColor groupTableViewBackgroundColor];
   
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*3, SCREEN_HEIGHT-40-(STATUS_BAR_HEIGHT+44)-44-self.tabBar_height);
//    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-44-self.tabBar_height);
    }];
    [_scrollView.superview layoutIfNeeded];
    
    
    category=[[GoodsCategoryView alloc]init];
    category.delageta=self;
    category.backgroundColor=[UIColor whiteColor];
    [_scrollView addSubview:category];
    
    [category mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView);
        make.left.equalTo(_scrollView).with.offset(0);
        make.height.offset(SCREEN_HEIGHT-40-(STATUS_BAR_HEIGHT+44)-44-self.tabBar_height);
        make.width.offset(SCREEN_WIDTH);
    }];
    
    update=[[UpdateView alloc]init];
    update.delageta=self;
    update.backgroundColor=[UIColor whiteColor];
    [_scrollView addSubview:update];
    
    [update mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView);
        make.left.equalTo(_scrollView).with.offset(SCREEN_WIDTH);
        make.height.offset(SCREEN_HEIGHT-40-(STATUS_BAR_HEIGHT+44)-44-self.tabBar_height);
        make.width.offset(SCREEN_WIDTH);
    }];
    
    discount=[[DiscountView alloc]init];
    discount.delageta=self;
    discount.backgroundColor=[UIColor whiteColor];
    [_scrollView addSubview:discount];
    
    [discount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView);
        make.left.equalTo(_scrollView).with.offset(SCREEN_WIDTH*2);
        make.height.offset(SCREEN_HEIGHT-40-(STATUS_BAR_HEIGHT+44)-44-self.tabBar_height);
        make.width.offset(SCREEN_WIDTH);
    }];
    
}
#pragma mark - 添加表格
-(void)addVIew{


    UIButton *category=[UIButton buttonWithType:UIButtonTypeCustom];
    [category setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [category setTitle:Localized(@"商品分类") forState:UIControlStateNormal];
    category.titleLabel.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:category];
    [category mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-self.tabBar_height);
        make.height.offset(44);
        make.width.offset((SCREEN_WIDTH)/2);
    }];
    
    UIButton *discount=[UIButton buttonWithType:UIButtonTypeCustom];
    [discount setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [discount setTitle:Localized(@"领券中心") forState:UIControlStateNormal];
    discount.titleLabel.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:discount];
    [discount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(category.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).offset(-self.tabBar_height);
        make.height.offset(44);
        make.width.offset((SCREEN_WIDTH)/2);
    }];
    
    UIView *line=[[UIView alloc]init];
    [self.view addSubview:line];
    line.backgroundColor=[UIColor colorWithHexString:@"#ECEBEC"];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(discount);
        make.height.offset(0.5);
    }];
    
    UIView *line2=[[UIView alloc]init];
    [self.view addSubview:line2];
    line2.backgroundColor=[UIColor colorWithHexString:@"#ECEBEC"];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.equalTo(discount);
        make.height.offset(0.5);
    }];
    
    UIView *Hline=[[UIView alloc]init];
    [self.view addSubview:Hline];
    Hline.backgroundColor=[UIColor colorWithHexString:@"#ECEBEC"];
    [Hline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-self.tabBar_height);
        make.top.equalTo(line);
        make.width.offset(0.5);
        make.centerX.offset(0);
    }];
}
#pragma mark-通过点击button来改变scrollview的偏移量
-(void)adjustScrollView:(NSInteger)index
{
    [UIView animateWithDuration:0.2 animations:^{
        _scrollView.contentOffset = CGPointMake(index*_scrollView.bounds.size.width, 0);
    }];
}

#pragma mark-选中scorllview来调整headvie的选中
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    [_headView setSelectAtIndex:index];
}

//#pragma mark 测试用
//-(void)changeScrollview:(NSInteger)index{
//
//    [UIView animateWithDuration:0 animations:^{
//        _scrollView.contentOffset = CGPointMake(index*_scrollView.bounds.size.width, 0);
//    }];
//
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
